package com.xtool.livewallpaper.video

import android.content.Context
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.view.SurfaceHolder
import com.xtool.livewallpaper.R
import com.xtool.livewallpaper.util.ToastUtil

class VideoPlayerView constructor(
    context: Context,
    attrs: AttributeSet? = null
) : GLSurfaceView(context, attrs), SurfaceHolder.Callback {
    private var mRenderer: GLWallpaperRenderer? = null
    private var mPlayer: SystemMediaPlayer? = null
    private var mVideoPath: Uri? = null
    private var isInitialed: Boolean = false
    fun init(player: SystemMediaPlayer, videoPath: Uri) {
        mPlayer = player
        initRender(player)
        setVideoPath(videoPath)
        holder.addCallback(this)
    }

    private fun initRender(player: SystemMediaPlayer) {
        setEGLContextClientVersion(3)
        mRenderer = GLES30WallpaperRenderer(context)
        preserveEGLContextOnPause = true
        setRenderer(mRenderer!!)
        renderMode = RENDERMODE_CONTINUOUSLY
        mRenderer?.setSourcePlayer(player)
    }

    private fun initVideo(path: Uri) {
        try {
            val mmr = MediaMetadataRetriever()
            mmr.setDataSource(context, path)
            val rotation = mmr.extractMetadata(
                MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION
            )?.toInt() ?: 0
            val width = mmr.extractMetadata(
                MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH
            )?.toInt() ?: 0
            val height = mmr.extractMetadata(
                MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT
            )?.toInt() ?: 0
            mmr.release()
            mRenderer?.setVideoSizeAndRotation(width, height, rotation)
        } catch (e: Exception) {
            e.printStackTrace()
            ToastUtil.show(R.string.invalid_video)
        }
    }

    fun setVideoPath(path: Uri) {
        initVideo(path)
        mVideoPath = path
        if (isInitialed) mPlayer?.play(path)
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        super.surfaceCreated(holder)
        val path = mVideoPath
        if (!isInitialed && path != null) {
            mPlayer?.play(path)
            isInitialed = true
        }
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) {
        super.surfaceChanged(holder, format, w, h)
        mRenderer?.setScreenSize(width, height)
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        super.surfaceDestroyed(holder)
        if (mPlayer?.isPlaying() == true) {
            mPlayer?.pause()
        }
    }

    fun start() {
        mPlayer?.start()
    }

    fun pause() {
        mPlayer?.pause()
    }

    fun stop() {
        mPlayer?.stop()
    }

    override fun onDetachedFromWindow() {
        mPlayer?.release()
        super.onDetachedFromWindow()
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        mRenderer?.setScreenSize(measuredWidth, measuredHeight)
    }
}